package com.zx.service.user.impl;

import com.zx.config.exception.BaseBusinessException;
import com.zx.config.exception.UserError;
import com.zx.dao.user.ReceivingMapper;
import com.zx.dto.request.user.ReceivingDtoRequest;
import com.zx.dto.response.user.ReceivingDtoResult;
import com.zx.model.user.User;
import com.zx.service.user.ReceivingService;
import com.zx.util.CookieUtil;
import com.zx.util.MallConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CookieValue;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author sunkai
 * @date 2022/8/17 0017 17:04
 * @mood zhou xun
 */
@Service
public class ReceivingServiceImpl implements ReceivingService {
    @Autowired
    private ReceivingMapper receivingMapper;

    @Override
    public List<ReceivingDtoResult> queryAddress(String userToken, ReceivingDtoRequest receivingDtoResult) {
            User user = MallConstant.MAP.get(userToken);
            receivingDtoResult.setUserId(user.getId());
        return receivingMapper.queryAddress(receivingDtoResult);
    }

    @Override
    public Boolean addAddress(ReceivingDtoRequest receivingDtoRequest) {
        Date time=new Date();
        receivingDtoRequest.setCreate_time(time);
        receivingDtoRequest.setUpdate_time(time);
        int i = receivingMapper.addAddress(receivingDtoRequest);
        if (i<=0){
            throw new BaseBusinessException(UserError.USER_ADDRESS_ERROR.getErrorCode(),UserError.USER_ADDRESS_ERROR.getErrorMessage());
        }
        return true;
    }

    @Override
    public Boolean updateAddress(@CookieValue(value = "UserToken") String userToken, ReceivingDtoRequest receivingDtoRequest) {
            User user = MallConstant.MAP.get(userToken);
            receivingDtoRequest.setUserId(user.getId());
            receivingDtoRequest.setUpdate_time(new Date());
        int i = receivingMapper.updateAddress(receivingDtoRequest);
        if (i<=0){
            throw new BaseBusinessException(UserError.USER_ADDRESS_ERROR.getErrorCode(),UserError.USER_ADDRESS_ERROR.getErrorMessage());
        }
        return true;
    }

    @Override
    public Boolean deleteAddress(ReceivingDtoRequest receivingDtoRequest) {
        int i = receivingMapper.deleteAddress(receivingDtoRequest);
        return i==0?false:true;
    }

}
